package com.un.ebs.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.un.ebs.core.util.SecurityUtils;
import com.un.ebs.core.util.WebResult;
import com.un.ebs.core.util.WebUtils;
import com.un.ebs.sys.domain.App;
import com.un.ebs.sys.dto.AppAddDto;
import com.un.ebs.sys.dto.AppUpdateDto;
import com.un.ebs.sys.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2019-09-15
 */
@RestController
@RequestMapping("/app")
@Api(value = "sys-app", description = "应用")
public class AppController {

    @Autowired
    private AppService service;

    @PostMapping("add")
    @ApiOperation(value = "add", notes = "添加")
    public WebResult save(@RequestBody AppAddDto req) {
        service.add(req);
        return WebUtils.success();
    }

    @PostMapping("update")
    @ApiOperation(value = "update", notes = "修改")
    public WebResult update(@RequestBody AppUpdateDto req) {
        service.update(req);
        return WebUtils.success();
    }

    @PostMapping("updateHomepageUrl")
    @ApiOperation(value = "updateHomepageUrl", notes = "更新首页配置")
    public WebResult updateHomepageUrl(@RequestParam String appId, @RequestParam String url, @RequestParam(required = false) String mobileUrl) {
        App obj = service.queryById(appId);
        obj.setHomepageUrl(url);
        obj.setHomepageMobileUrl(mobileUrl);
        obj.updateById();
        return WebUtils.success();
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "getById", notes = "根据主键获取单个 App")
    public WebResult getById(@PathVariable("id") String id) {
        return WebUtils.success(service.queryById(id));
    }

    @GetMapping("list")
    @ApiOperation(value = "list", notes = "获取开发者的APP")
    public WebResult list() {
        LambdaQueryWrapper<App> lq = Wrappers.lambdaQuery();
        lq.eq(App::getCreateAt, SecurityUtils.getUserContent().getId());
        return WebUtils.success(service.list(lq));
    }

    @PostMapping("delete/{id}")
    @ApiOperation(value = "delete", notes = "根据主键删除单个 App")
    public WebResult delete(@PathVariable("id") String id) {
        service.delete(id);
        return WebUtils.success();
    }

}